<?php

class Mithos_Character {

    private $_name;
    private $_account;
    private $_avatar;
    private $_race;
    private $_level;
    private $_experience;
    private $_points;
    private $_strength;
    private $_agility;
    private $_energy;
    private $_vitality;
    private $_command;
    private $_money;
    private $_map;
    private $_x;
    private $_y;
    private $_guild;
    private $_inventory;
    private $_data = array();

    public function __construct($name = null) {
        if ($name !== null) {
            $this->read($name);
        }
    }

    public function name($name = null) {
        if ($name === null) {
            return $this->_name;
        }
        $this->_name = $name;
        return $this;
    }

    public function avatar($avatar = null) {
        if ($avatar === null) {
            return $this->_avatar;
        }
        $this->_avatar = $avatar;
        return $this;
    }

    public function account($account = null) {
        if ($account === null) {
            if (!$this->_account instanceof Mithos_Account) {
                return new Mithos_Account($this->_account);
            }
            return $this->_account;
        }
        $this->_account = $account;
        return $this;
    }

    public function race($race = null) {
        if ($race === null) {
            return $this->_race;
        }
        $this->_race = $race;
        return $this;
    }

    public function level($level = null) {
        if ($level === null) {
            return $this->_level;
        }
        $this->_level = $level;
        return $this;
    }

    public function experience($experience = null) {
        if ($experience === null) {
            return $this->_experience;
        }
        $this->_experience = $experience;
        return $this;
    }

    public function points($points = null) {
        if ($points === null) {
            return $this->_points;
        }
        $this->_points = $points;
        return $this;
    }

    public function strength($strength = null) {
        if ($strength === null) {
            return $this->_strength;
        }
        $this->_strength = $strength;
        return $this;
    }

    public function agility($agility = null) {
        if ($agility === null) {
            return $this->_agility;
        }
        $this->_agility = $agility;
        return $this;
    }

    public function energy($energy = null) {
        if ($energy === null) {
            return $this->_energy;
        }
        $this->_energy = $energy;
        return $this;
    }

    public function vitality($vitality = null) {
        if ($vitality === null) {
            return $this->_vitality;
        }
        $this->_vitality = $vitality;
        return $this;
    }

    public function command($command = null) {
        if ($command === null) {
            return $this->_command;
        }
        $this->_command = $commande;
        return $this;
    }

    public function money($money = null) {
        if ($money === null) {
            return $this->_money;
        }
        $this->_money = $money;
        return $this;
    }

    public function map($map = null) {
        if ($map === null) {
            return $this->_map;
        }
        $this->_map = $map;
        return $this;
    }

    public function x($x = null) {
        if ($x === null) {
            return $this->_x;
        }
        $this->_x = $x;
        return $this;
    }

    public function y($y = null) {
        if ($y === null) {
            return $this->_y;
        }
        $this->_y = $y;
        return $this;
    }

    public function guild($guild = null) {
        if ($guild === null) {
            if (!$this->_guild instanceof Mithos_Guild) {
                return new Mithos_Guild($this->_guild);
            }
            return $this->_guild;
        }
        $this->_guild = $guild;
        return $this;
    }

    public function inventory($inventory = null) {
        if ($inventory === null) {
            return $this->_inventory;
        }
        $this->_inventory = $inventory;
        return $this;
    }

    public function data($field = null) {
        if ($field === null) {
            return $this->_data;
        }
        return $this->_data[$field];
    }

    public function count($where = null) {
        $where = $where !== null ? ' WHERE ' . $where : '';
        $result = Mithos_Mssql::getInstance()->fetch('SELECT 
			COUNT(1) AS total FROM Character
		' . $where);
        return $result['total'];
    }

    public function team() {
        $results = Mithos_Mssql::getInstance()->fetchAll('SELECT
			MEMB_STAT.ConnectStat,
			AccountCharacter.GameIDC,
			Character.Name AS name
			FROM MEMB_STAT 
			INNER JOIN AccountCharacter ON (MEMB_STAT.memb___id = AccountCharacter.ID) 
			INNER JOIN Character ON (MEMB_STAT.memb___id = Character.AccountID) 
			WHERE Character.CtlCode > 7
		');
        if (!empty($resultss)) {
            foreach ($results as $key => $value) {
                $results[$key]['status'] = $results[$key]['ConnectStat'] > 0 && $results[$key]['GameIDC'] == $results[$key]['name'] ? 1 : 0;
                unset($results[$key]['ConnectStat'], $results[$key]['GameIDC']);
            }
        }
        return $results;
    }

    public function read($name) {
        $result = Mithos_Mssql::getInstance()->fetch('SELECT
			s.ConnectStat AS connect_stat,
			s.ConnectTM AS last_connection,
			s.ServerName AS server,
			ac.GameIDC AS game_idc,
			c.Name AS name,
			c.Avatar AS avatar,
			c.cLevel AS level,
			c.Experience AS experience,
			c.Class AS race,
			c.PkLevel AS pk_level,
			c.PkCount AS pk_count,
			c.AccountID AS account,
			c.LevelUpPoint AS points,
			c.MapNumber AS map,
			c.MapPosY AS y,
			c.MapPosX AS x,
			c.CtlCode AS code,
			c.Strength AS strength,
			c.Dexterity AS agility,
			c.Vitality AS vitality,
			c.Energy AS energy,
			c.Money AS money,
			gm.G_Name AS guild
			FROM Character c
			LEFT JOIN AccountCharacter ac ON c.AccountID = ac.ID COLLATE DATABASE_DEFAULT
			LEFT JOIN MEMB_STAT s ON c.AccountID = s.memb___id COLLATE DATABASE_DEFAULT
			LEFT JOIN GuildMember gm ON c.Name = gm.Name COLLATE DATABASE_DEFAULT
			WHERE c.Name = :name[string]
		', array('name' => $name));
        if (!empty($result)) {
            $this->_data = $result;
            foreach ($result as $key => $value) {
                if (method_exists($this, $key)) {
                    $this->{$key}($value);
                }
            }
        }
    }

}

?>